package cn.gcyl.module.biz.controller.admin.usersubscriptionwordslog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

import cn.gcyl.framework.excel.core.util.ExcelUtils;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gcyl.module.biz.controller.admin.usersubscriptionwordslog.vo.*;
import cn.gcyl.module.biz.dal.dataobject.usersubscriptionwordslog.UserSubscriptionWordsLogDO;
import cn.gcyl.module.biz.service.usersubscriptionwordslog.UserSubscriptionWordsLogService;

@Tag(name = "管理后台 - 用户订阅词记录")
@RestController
@RequestMapping("/biz/user-subscription-words-log")
@Validated
public class UserSubscriptionWordsLogController {

    @Resource
    private UserSubscriptionWordsLogService userSubscriptionWordsLogService;

    @PostMapping("/create")
    @Operation(summary = "创建用户订阅词记录")
//    @PreAuthorize("@ss.hasPermission('biz:user-subscription-words-log:create')")
    public CommonResult<Long> createUserSubscriptionWordsLog(@Valid @RequestBody UserSubscriptionWordsLogSaveReqVO createReqVO) {
        return success(userSubscriptionWordsLogService.createUserSubscriptionWordsLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户订阅词记录")
//    @PreAuthorize("@ss.hasPermission('biz:user-subscription-words-log:update')")
    public CommonResult<Boolean> updateUserSubscriptionWordsLog(@Valid @RequestBody UserSubscriptionWordsLogSaveReqVO updateReqVO) {
        userSubscriptionWordsLogService.updateUserSubscriptionWordsLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户订阅词记录")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:user-subscription-words-log:delete')")
    public CommonResult<Boolean> deleteUserSubscriptionWordsLog(@RequestParam("id") Long id) {
        userSubscriptionWordsLogService.deleteUserSubscriptionWordsLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户订阅词记录")
//                @PreAuthorize("@ss.hasPermission('biz:user-subscription-words-log:delete')")
    public CommonResult<Boolean> deleteUserSubscriptionWordsLogList(@RequestParam("ids") List<Long> ids) {
        userSubscriptionWordsLogService.deleteUserSubscriptionWordsLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户订阅词记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:user-subscription-words-log:query')")
    public CommonResult<UserSubscriptionWordsLogRespVO> getUserSubscriptionWordsLog(@RequestParam("id") Long id) {
        UserSubscriptionWordsLogDO userSubscriptionWordsLog = userSubscriptionWordsLogService.getUserSubscriptionWordsLog(id);
        return success(BeanUtils.toBean(userSubscriptionWordsLog, UserSubscriptionWordsLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户订阅词记录分页")
//    @PreAuthorize("@ss.hasPermission('biz:user-subscription-words-log:query')")
    public CommonResult<PageResult<UserSubscriptionWordsLogRespVO>> getUserSubscriptionWordsLogPage(@Valid UserSubscriptionWordsLogPageReqVO pageReqVO) {
        PageResult<UserSubscriptionWordsLogDO> pageResult = userSubscriptionWordsLogService.getUserSubscriptionWordsLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserSubscriptionWordsLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户订阅词记录 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:user-subscription-words-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserSubscriptionWordsLogExcel(@Valid UserSubscriptionWordsLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserSubscriptionWordsLogDO> list = userSubscriptionWordsLogService.getUserSubscriptionWordsLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户订阅词记录.xls", "数据", UserSubscriptionWordsLogRespVO.class,
                        BeanUtils.toBean(list, UserSubscriptionWordsLogRespVO.class));
    }

}